<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { loginService } from '@/api/user.js'
import { useUserStore } from '@/stores/index.js'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const validateLoginForm = () => {
  if (!loginForm.username) {
    ElMessage.warning('请输入用户名')
    return false
  }
  if (!loginForm.password) {
    ElMessage.warning('请输入密码')
    return false
  }
  if (loginForm.password.length < 6) {
    ElMessage.warning('密码长度至少6位')
    return false
  }
  return true
}


// 处理登录
const handleLogin = async () => {
  if (!validateLoginForm()) return
  
  loading.value = true
  try {
    const response = await loginService(loginForm)
    if (response.data.code === 1) {
      // 保存token和用户信息
      userStore.setToken(response.data.data.token || 'mock-token')
      userStore.setUser(response.data.data.user || response.data.data)
      
      ElMessage.success('登录成功！')
      
      // 根据用户权限跳转到不同页面
      const authType = response.data.data.user?.authType || response.data.data.authType
      if (authType === 3 || authType === 4) {
        // 人事管理员或财务管理员
        router.push('/Hr')
      } else if (authType === 2) {
        // 审批经理
        router.push('/review')
      } else {
        // 普通员工
        router.push('/apply')
      }
    } else {
      ElMessage.error(response.data.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.msg || '登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}


// 快速登录（开发测试用）
const quickLogin = (role) => {
  if (role === 'admin') {
    loginForm.username = 'admin'
    loginForm.password = '123456'
  } else if (role === 'manager') {
    loginForm.username = 'manager'
    loginForm.password = '123456'
  } else {
    loginForm.username = 'user'
    loginForm.password = '123456'
  }
  handleLogin()
}
</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 左侧装饰区域 -->
      <div class="login-left">
        <div class="logo-section">
          <div class="logo-icon">🏢</div>
          <h1 class="logo-title">差旅管理系统</h1>
          <p class="logo-subtitle">Travel Management System</p>
        </div>
        <div class="feature-list">
          <div class="feature-item">
            <span class="feature-icon">✈️</span>
            <span class="feature-text">便捷的差旅申请</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">📊</span>
            <span class="feature-text">智能审批流程</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">💰</span>
            <span class="feature-text">费用管理透明</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">👥</span>
            <span class="feature-text">人事管理高效</span>
          </div>
        </div>
      </div>

      <!-- 右侧表单区域 -->
      <div class="login-right">
        <div class="form-container">
          <!-- 标题 -->
          <div class="form-header">
            <h2 class="form-title">欢迎登录</h2>
            <p class="form-subtitle">登录您的账户以继续</p>
          </div>

          <!-- 登录表单 -->
          <div class="form-content">
            <div class="form-group">
              <label class="form-label">
                <span class="label-icon">👤</span>
                用户名
              </label>
              <input
                v-model="loginForm.username"
                type="text"
                class="form-input"
                placeholder="请输入用户名"
                @keyup.enter="handleLogin"
              />
            </div>

            <div class="form-group">
              <label class="form-label">
                <span class="label-icon">🔒</span>
                密码
              </label>
              <input
                v-model="loginForm.password"
                type="password"
                class="form-input"
                placeholder="请输入密码"
                @keyup.enter="handleLogin"
              />
            </div>

            <div class="form-actions">
              <label class="remember-me">
                <input type="checkbox" />
                <span>记住我</span>
              </label>
              <a href="#" class="forgot-password">忘记密码？</a>
            </div>

            <button
              class="submit-btn"
              :class="{ loading: loading }"
              :disabled="loading"
              @click="handleLogin"
            >
              <span v-if="!loading">登录</span>
              <span v-else class="loading-spinner">登录中...</span>
            </button>

            <!-- 快速登录按钮（开发测试用） -->
            <div class="quick-login">
              <p class="quick-login-title">快速登录（测试）</p>
              <div class="quick-login-buttons">
                <button class="quick-btn" @click="quickLogin('admin')">管理员</button>
                <button class="quick-btn" @click="quickLogin('manager')">经理</button>
                <button class="quick-btn" @click="quickLogin('user')">员工</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 页脚 -->
    <div class="login-footer">
      <p>&copy; 2025 差旅管理系统. All rights reserved.</p>
    </div>
  </div>
</template>

<style scoped>
/* 容器样式 */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s infinite ease-in-out;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  right: -50px;
  animation-delay: 5s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: 10%;
  animation-delay: 10s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 登录卡片 */
.login-card {
  display: flex;
  width: 100%;
  max-width: 1000px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  position: relative;
  z-index: 1;
  animation: slideIn 0.5s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 左侧装饰区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60px 40px;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.logo-section {
  text-align: center;
  margin-bottom: 60px;
}

.logo-icon {
  font-size: 80px;
  margin-bottom: 20px;
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.logo-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 10px 0;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.logo-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
  letter-spacing: 2px;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateX(10px);
}

.feature-icon {
  font-size: 24px;
}

.feature-text {
  font-size: 16px;
  font-weight: 500;
}

/* 右侧表单区域 */
.login-right {
  flex: 1;
  padding: 60px 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-container {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-title {
  font-size: 32px;
  font-weight: 700;
  color: #333;
  margin: 0 0 10px 0;
}

.form-subtitle {
  font-size: 14px;
  color: #999;
  margin: 0;
}

/* 表单内容 */
.form-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #555;
}

.label-icon {
  font-size: 16px;
}

.form-input,
.form-select {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e8e8e8;
  border-radius: 10px;
  font-size: 14px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input::placeholder {
  color: #aaa;
}

/* 表单操作 */
.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: #666;
}

.remember-me input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #764ba2;
}

/* 提交按钮 */
.submit-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.submit-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.submit-btn:active:not(:disabled) {
  transform: translateY(0);
}

.submit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.loading-spinner {
  display: inline-block;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 快速登录 */
.quick-login {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}

.quick-login-title {
  font-size: 12px;
  color: #999;
  text-align: center;
  margin: 0 0 10px 0;
}

.quick-login-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.quick-btn {
  padding: 6px 12px;
  background: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.quick-btn:hover {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

/* 表单底部 */
.form-footer {
  margin-top: 30px;
  text-align: center;
}

.toggle-text {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.toggle-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
  transition: color 0.3s ease;
}

.toggle-link:hover {
  color: #764ba2;
}

/* 页脚 */
.login-footer {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  color: white;
  font-size: 12px;
  opacity: 0.8;
  z-index: 1;
}

.login-footer p {
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    flex-direction: column;
    max-width: 500px;
  }

  .login-left {
    padding: 40px 30px;
  }

  .logo-icon {
    font-size: 60px;
  }

  .logo-title {
    font-size: 24px;
  }

  .feature-list {
    display: none;
  }

  .login-right {
    padding: 40px 30px;
  }

  .form-title {
    font-size: 24px;
  }
}
</style>
